package top.soulblack.purchasesale.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import top.soulblack.purchasesale.model.Goods;
import top.soulblack.purchasesale.model.PurchaseForm;
import top.soulblack.purchasesale.model.VO.GoodsToIdVO;
import top.soulblack.purchasesale.model.VO.PurchaseFormVO;
import top.soulblack.purchasesale.service.GoodsService;
import top.soulblack.purchasesale.service.PurchaseFormService;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * create by 小川
 */

@Controller
@RequestMapping("/forms")
public class FormsController {

    @Autowired
    private PurchaseFormService purchaseFormService;

    @RequestMapping("/purchase")
    public String purchase(HttpServletRequest request, Model model){

        Integer sum = 0;
        BigDecimal amount = new BigDecimal(0);
        String amounts = null;
        List<PurchaseFormVO> purchaseFormVOS = null;
        List<PurchaseFormVO> purchaseFormVOList = purchaseFormService.selectForUnit();
        if (request.getParameter("startTime") !="" && request.getParameter("startTime") != null
        && request.getParameter("endTime") != "" && request.getParameter("endTime") != null){
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
           purchaseFormVOS = purchaseFormService.selectClearByTime(startTime,endTime);
        }else{
            purchaseFormVOS = purchaseFormService.selectClear();
        }
        //计算总量与总金额
        for (PurchaseFormVO purchaseForm: purchaseFormVOS ){
            sum += purchaseForm.getUnitNumber();
            BigDecimal i = new BigDecimal(purchaseForm.getUnitNumber());
            amount = amount.add(i.multiply(purchaseForm.getUnitPrice()));
        }
        DecimalFormat Amount = new DecimalFormat("#,###.00");
        amounts = Amount.format(amount);
        model.addAttribute("sum",sum);
        model.addAttribute("amount",amounts);
        model.addAttribute("goods",purchaseFormVOS);
        model.addAttribute("good",purchaseFormVOList);
        return "forms/purchase";
    }

    @RequestMapping("/sell")
    public String sell(Model model,HttpServletRequest request){

        Integer sum = 0;
        BigDecimal amount = new BigDecimal(0);
        String amounts = null;
        List<PurchaseForm> purchaseFormList = purchaseFormService.selectAll();
        List<PurchaseFormVO> purchaseFormVOS = purchaseFormService.selectClear();
        List<PurchaseFormVO> purchaseFormVOList = purchaseFormService.selectForUnit();

        //计算总量与总金额
        for (PurchaseFormVO purchaseForm: purchaseFormVOS ){
            sum += purchaseForm.getUnitNumber();
            BigDecimal i = new BigDecimal(purchaseForm.getUnitNumber());
            amount = amount.add(i.multiply(purchaseForm.getUnitPrice()));
        }
        DecimalFormat Amount = new DecimalFormat("#,###.00");
        amounts = Amount.format(amount);
        model.addAttribute("sum",sum);
        model.addAttribute("amount",amounts);
        model.addAttribute("goods",purchaseFormVOS);
        model.addAttribute("good",purchaseFormVOList);
        return "forms/sell";
    }

    @RequestMapping("/custom_sale")
    public String custom_sale(){
        return "forms/custom_sale";
    }

    @RequestMapping("/goods_sale")
    public String goods_sale(){
        return "forms/goods_sale";
    }

    @RequestMapping("/check")
    public String check(){
        return "forms/check";
    }

    //调拨
    @RequestMapping("/allot")
    public String allot(){
        return "forms/allot";
    }

    @RequestMapping("/operation_summary")
    public String operation_summary(){
        return "forms/operation_summary";
    }

    @RequestMapping("/goods_summary")
    public String goods_summary(){
        return "forms/goods_summary";
    }

    @RequestMapping("/purchase_mobile")
    public String purchase_mobile(Model model,HttpServletRequest request){

        //获取时间
        //可以对每个时间域单独修改
        Date d = new Date();
        SimpleDateFormat sfYear = new SimpleDateFormat("yyyy");
        SimpleDateFormat sfMonth = new SimpleDateFormat("MM");
        SimpleDateFormat sfDay = new SimpleDateFormat("dd");
        int year = Integer.valueOf(sfYear.format(d));
        int month = Integer.valueOf(sfMonth.format(d));
        int day = Integer.valueOf(sfDay.format(d));
        int day1 = day+1;
        int day2 = day-1;
        Integer sum = 0;
        BigDecimal amount = new BigDecimal(0);
        String amounts = null;
        List<PurchaseFormVO> purchaseFormVOS = null;
        List<PurchaseFormVO> purchaseFormVOList = purchaseFormService.selectForUnit();
        if (request.getParameter("startTime") !="" && request.getParameter("startTime") != null
                && request.getParameter("endTime") != "" && request.getParameter("endTime") != null){
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            purchaseFormVOS = purchaseFormService.selectClearByTime(startTime,endTime);
        }else{
            purchaseFormVOS = purchaseFormService.selectClearByTime(year + "-" + month + "-" + day, year + "-" + month + "-" + day1);
        }
        //计算总量与总金额
        for (PurchaseFormVO purchaseForm: purchaseFormVOS ){
            sum += purchaseForm.getUnitNumber();
            BigDecimal i = new BigDecimal(purchaseForm.getUnitNumber());
            amount = amount.add(i.multiply(purchaseForm.getUnitPrice()));
        }
        DecimalFormat Amount = new DecimalFormat("#,###.00");
        amounts = Amount.format(amount);
        model.addAttribute("sum",sum);
        model.addAttribute("amount",amounts);
        model.addAttribute("goods",purchaseFormVOS);
        model.addAttribute("good",purchaseFormVOList);
        return "forms/purchase_mobile";
    }
}
